package com.rabc.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * Security权限配置
 */
@Configuration
public class SecurityConfig   {





    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
    /**TODO 后期修改需要只暴露登录 注册接口
     * 配置Security过滤器链
     * @param http HttpSecurity对象
     * @return SecurityFilterChain
     * @throws Exception 如果配置中出现问题抛出异常
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                // 关闭csrf保护
                .csrf().disable()
                // 允许所有请求无需身份验证或授权
                .authorizeRequests()
                .anyRequest().permitAll();

        // 配置表单登录
        http.formLogin()
                // 登录页面
                .loginPage("/toLogin")
                // 登录处理URL
                .loginProcessingUrl("/user/login");

        // 如果有登出需求，可以配置登出URL
        // http.logout().logoutUrl("/logout");

        // 返回构建好的SecurityFilterChain
        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}